package com.urbanflow.vinci.bot;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import com.urbanflow.vinci.controler.CheminNotFound;
import com.urbanflow.vinci.controler.DialogManager;
import com.urbanflow.vinci.controler.ParcoursManager;
import com.urbanflow.vinci.initializator.Initializator;
import com.urbanflow.vinci.main.Arret;
import com.urbanflow.vinci.main.Horaire;
import com.urbanflow.vinci.parcourt.Chemin;

public class Bot {
	private Arret depart;
	private Arret destination;
	private List<Horaire> mesHoraires;
	private Boolean transporte;
	private String botToken;
	private String botSecret;
	private Chemin leCheminPrevu;
	private DialogManager dManager;
	private Date dateRevu;
	
	public Bot(String botTok, String botSe) {
		botToken = botTok;
		botSecret = botSe;
	}
	
	public Arret getDepart() {
		return depart;
	}
	public void setDepart(Arret depart) {
		this.depart = depart;
	}
	public Arret getDestination() {
		return destination;
	}
	public void setDestination(Arret destination) {
		this.destination = destination;
	}
	public List<Horaire> getMesHoraires() {
		return mesHoraires;
	}
	public void setMesHoraires(List<Horaire> mesHoraires) {
		this.mesHoraires = mesHoraires;
	}
	public Boolean getTransporte() {
		return transporte;
	}
	public void setTransporte(Boolean transporte) {
		this.transporte = transporte;
	}
	public String getBotToken() {
		return botToken;
	}
	public void setBotToken(String botToken) {
		this.botToken = botToken;
	}
	public String getBotSecret() {
		return botSecret;
	}
	public void setBotSecret(String botSecret) {
		this.botSecret = botSecret;
	}

	public Chemin getLeCheminPrevu() {
		return leCheminPrevu;
	}

	public void setLeCheminPrevu(Chemin leCheminPrevu) {
		this.leCheminPrevu = leCheminPrevu;
	}

	public Boolean move() throws CheminNotFound {
		if(dManager.MoveBot(leCheminPrevu.getMoyen().getIdLigne(), leCheminPrevu.getArrive().getIdArret().toString(), leCheminPrevu.getLhoraire().getDate())) {
			if(dManager.Success) { 
				prendreEnCompteDeplacement();
				return move();
			}
			if(dManager.Rerouted) {
				prendreEnCompteReroutage();
				return move();
			}
			if(dManager.VeniVidiVinci) {
				return true;
			}
		} else {
			return prendreEnComptePenalite();
			}
		return false;
	}
	
	public void prendreEnCompteDeplacement() throws CheminNotFound{	
		Date datePrevision = leCheminPrevu.getLhoraire().getDate();
		leCheminPrevu = leCheminPrevu.getCheminSuivant();
		Integer idDepart = new Integer(dManager.IdArretDepart);
		Arret debut = Initializator.getInstance().getArret(idDepart);	
		depart = debut;
		if(!datePrevision.equals(dManager.HeureActuelle)) {
			recalculTrajet();
		}
	
	}
	
	public void prendreEnCompteReroutage() throws CheminNotFound{
		Integer idFin = new Integer(dManager.IdArretDestination);
		Arret fin = Initializator.getInstance().getArret(idFin);
		destination = fin;
		Integer idDepart = new Integer(dManager.IdArretDepart);
		Arret debut = Initializator.getInstance().getArret(idDepart);	
		depart = debut;
		recalculTrajet();
	}
	
	public void recalculTrajet() throws CheminNotFound{
		ParcoursManager.getInstance().creerLesTrajets(depart, destination, dManager.HeureActuelle);
		ParcoursManager.getInstance().reduireLeChemin();
		this.leCheminPrevu= ParcoursManager.getInstance().getLeCheminPrevu();
	}
	public Boolean prendreEnComptePenalite() throws CheminNotFound {
//		Calendar c = new GregorianCalendar((Date)this.leCheminPrevu.getLhoraire().getDate());
//		c.add(Calendar.MINUTE,dManager.DateDepart);
//		date = c.getTime();
		dManager.GetIncidents();
		recalculTrajet();
		return move();
	}

	public DialogManager getdManager() {
		return dManager;
	}

	public void setdManager(DialogManager dManager) {
		this.dManager = dManager;
	}

}
